
#ifndef ChainedHashTable_
#define ChainedHashTable_

#include <iostream>
using namespace std;

#include "AlogException.h"
#include "SortedChain.h"

template<class E, class K>
class ChainHashTable {
  public:
    ChainHashTable( int divisor = 11 )
    {
        D = divisor;
        ht = new SortedChain<E, K>[D];
    }

    ~ChainHashTable() { delete [] ht; }

    bool Search( const K& k, E& e ) const
    {
        return ht[k % D].Search( k, e );
    }

    ChainHashTable<E, K>& Insert( const E& e )
    {
        ht[e % D].DistinctInsert( e );

        return *this;
    }

    ChainHashTable<E, K>& Delete( const K& k, E& e )
    {
        ht[k % D].Delete( k, e );

        return *this;
    }

    void Output() const;

  private:
    int D;
    SortedChain<E, K>* ht;
};


template<class E, class K>
void ChainHashTable<E, K>::Output() const
{
    for ( int i = 0; i < D; i++ ) {
        cout << "Chain " << i << ' ';
        ht[i].Output( cout );
        cout << endl;
    }
}

#endif

